package org.jeecg.common.util.xinyuan;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class FileEncryptor {
    private static final String AES = "AES";
    private static final String ENCRYPTED_FILE_EXTENSION = ".encrypted";

    public static void encryptFile(String inputFile, String outputFile, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), AES);
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        try (FileInputStream inputStream = new FileInputStream(inputFile);
             FileOutputStream outputStream = new FileOutputStream(outputFile)) {
            CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                cipherOutputStream.write(buffer, 0, bytesRead);
            }
            cipherOutputStream.flush();
        }
    }

    public static void main(String[] args) {
//        String inputFile = "D:/test/test.pdf";
//        String outputFile = "D:/test/test2.pdf";
        String inputFile = "D:/test/project.json";
        String outputFile = "D:/test/project2.json";
        String key = "WVKYTdykOM2yI/tY";

        try {
            encryptFile(inputFile, outputFile, key);
            System.out.println("File encrypted successfully.");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {
            e.printStackTrace();
        }
    }
}
